package com.chen.palmar.project.set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.view.AutoToolbar;
import com.chen.palmar.entity.AuthEntity;
import com.primb.androidlibs.net.converter.RxSchedulerUtils;
import com.primb.androidlibs.net.entity.HttpResultEntity;
import com.primb.androidlibs.utils.RegexUtils;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.schedulers.Schedulers;

/**
 * Created by chen on 2018/1/8.
 */

public class EditPhoneActivity extends BaseActivity {

    @Bind(R.id.title_bar)
    TextView titleBar;
    @Bind(R.id.tv_top)
    TextView tvTop;
    @Bind(R.id.tool_bar)
    AutoToolbar toolBar;
    @Bind(R.id.et_edit_phone)
    EditText etEditPhone;
    @Bind(R.id.tv_auth_code)
    TextView tvAuthCode;
    @Bind(R.id.et_auth_code)
    EditText etAuthCode;
    private String info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {
        info = getIntent().getStringExtra("info");

        toolBar.setNavigationOnClickListener(v -> EditPhoneActivity.this.finish());

        titleBar.setText("绑定手机号");

        etEditPhone.setText(TextUtils.isEmpty(info) ? "" : info);

        tvTop.setText("保存");
        tvTop.setVisibility(View.VISIBLE);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_phone;
    }


    private void commitNewPhone(String phone, String auth) {
        HashMap<String, String> map = new HashMap<>();
        map.put("newMobile", phone);
        map.put("verify_code", auth);
        map.put("mobile", info);
        map.put("code_type", "4");
        Subscription subscribe = DataCenter.editUserPhone(map).subscribe(new HttpSubscriber<HttpResultEntity>(this, showLoading()) {
            @Override
            public void onNext(HttpResultEntity httpResultEntity) {
                if (!TextUtils.isEmpty(httpResultEntity.getMessage())) {
                    showToast(httpResultEntity.getMessage());
                    Intent intent = new Intent();
                    intent.putExtra("info", phone);
                    setResult(Activity.RESULT_OK, intent);
                    EditPhoneActivity.this.finish();
                }
            }
        });
        subscription.add(subscribe);
    }

    @OnClick({R.id.tv_top, R.id.tv_auth_code})
    public void onViewClicked(View view) {
        if (TextUtils.isEmpty(etEditPhone.getText().toString())) {
            showToast("请输入手机号");
            return;
        }
        switch (view.getId()) {
            case R.id.tv_top:
                if (TextUtils.isEmpty(etAuthCode.getText().toString())) {
                    showToast("请输入验证码");
                    return;
                }
                commitNewPhone(etEditPhone.getText().toString(), etAuthCode.getText().toString());
                break;
            case R.id.tv_auth_code:
                if (!RegexUtils.isMobileExact(etEditPhone.getText())) {
                    showToast("请输入正确格式手机号");
                    return;
                }
                getAuthCode(etEditPhone.getText().toString());
                break;
        }
    }

    private void getAuthCode(String phone) {
        tvAuthCode.setEnabled(false);
        Subscription subscribe = Observable.interval(1, TimeUnit.SECONDS, Schedulers.io()).take(59).compose(RxSchedulerUtils.normalSchedulersTransformer()).subscribe(new Subscriber<Long>() {
            @Override
            public void onCompleted() {
                tvAuthCode.setEnabled(true);
                tvAuthCode.setText("重新获取验证码");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Long aLong) {
                tvAuthCode.setText((59 - aLong) + "秒");
            }
        });
        subscription.add(subscribe);

        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("code_type", "4");
        Subscription auth = DataCenter.getAuthCode(map).subscribe(new HttpSubscriber<AuthEntity>(this, null) {
            @Override
            public void onNext(AuthEntity o) {
                if (!TextUtils.isEmpty(o.getMessage())) {
                    showToast(o.getMessage());
                }
            }
        });
        subscription.add(auth);
    }
}
